
"""
    角色
"""

from rest_framework import serializers
from users.models import Organization, Role


class RoleSerializer(serializers.Serializer):
    id = serializers.IntegerField()
    label = serializers.CharField(max_length=20, source='name')


class OrganizationRoleTreeSerializer(serializers.ModelSerializer):
    """
    组织架构关联角色目录树
    """
    label = serializers.StringRelatedField(source='name')
    children = RoleSerializer(many=True, read_only=True, source='role_set')

    class Meta:
        model = Organization
        fields = ('id', 'label', 'pid', 'children')


class RoleModelSerializer(serializers.ModelSerializer):

    class Meta:
        model = Role
        fields = '__all__'


class RoleModelRetrieveViewSerializer(serializers.ModelSerializer):

    class Meta:
        model = Role
        fields = '__all__'

    def to_representation(self, instance):
        """
        修改序列化前的数据格式
        :param instance:
        :return:
        """
        data = super().to_representation(instance)
        if data['department']:
            department = Organization.objects.filter(id=data['department']).first()
            if department:
                data['department'] = [department.id]
                if department.pid:
                    data['department'] = [department.pid.id, department.id]
                    if department.pid.pid:
                        data['department'] = [department.pid.pid.id, department.pid.id, department.id]
                        if department.pid.pid.pid:
                            data['department'] = [department.pid.pid.pid.id, department.pid.pid.id, department.pid.id,
                                                  department.id]
                            if department.pid.pid.pid.pid:
                                data['department'] = [department.pid.pid.pid.pid.id, department.pid.pid.pid.id,
                                                      department.pid.pid.id, department.pid.id, department.id]
                                if department.pid.pid.pid.pid.pid:
                                    data['department'] = [department.pid.pid.pid.pid.pid.id,
                                                          department.pid.pid.pid.pid.id,
                                                          department.pid.pid.pid.id, department.pid.pid.id,
                                                          department.pid.id,
                                                          department.id]
                                    if department.pid.pid.pid.pid.pid.pid:
                                        data['department'] = [department.pid.pid.pid.pid.pid.pid.id,
                                                              department.pid.pid.pid.pid.pid.id,
                                                              department.pid.pid.pid.pid.id,
                                                              department.pid.pid.pid.id, department.pid.pid.id,
                                                              department.pid.id,
                                                              department.id]
        return data


class RoleModelListSerializer(serializers.ModelSerializer):

    class Meta:
        model = Role
        fields = '__all__'

    def to_representation(self, instance):
        """
        修改序列化前的数据格式
        :param instance:
        :return:
        """
        data = super().to_representation(instance)
        if data['department']:
            did = Role.objects.filter(id=data['id']).first()
            if did:
                data['department'] = str(did.department)
                if did.department.pid:
                    data['department'] = str(did.department.pid) + ' / ' + str(did.department)
                    if did.department.pid.pid:
                        data['department'] = str(did.department.pid.pid) + ' / ' + str(did.department.pid) + ' / ' + str(did.department)
                        if did.department.pid.pid.pid:
                            data['department'] = str(did.department.pid.pid.pid) + ' / ' + str(did.department.pid.pid) + ' / ' + str(did.department.pid) + ' / ' + str(did.department)
                            if did.department.pid.pid.pid.pid:
                                data['department'] = str(did.department.pid.pid.pid.pid) + ' / ' + str(did.department.pid.pid.pid) + ' / ' + str(did.department.pid.pid) + ' / ' + str(did.department.pid) + ' / ' + str(did.department)
                                if did.department.pid.pid.pid.pid.pid:
                                    data['department'] = str(did.department.pid.pid.pid.pid.pid) + ' / ' + str(did.department.pid.pid.pid.pid) + ' / ' + str(did.department.pid.pid.pid) + ' / ' + str(did.department.pid.pid) + ' / ' + str(did.department.pid) + ' / ' + str(did.department)
                                    if did.department.pid.pid.pid.pid.pid.pid:
                                        data['department'] = str(did.department.pid.pid.pid.pid.pid.pid) + ' / ' + str(did.department.pid.pid.pid.pid.pid) + ' / ' + str(did.department.pid.pid.pid.pid) + ' / ' + str(did.department.pid.pid.pid) + ' / ' + str(did.department.pid.pid) + ' / ' + str(did.department.pid) + ' / ' + str(did.department)
        return data


class RoleModelExportSerializer(serializers.ModelSerializer):

    class Meta:
        model = Role
        fields = ('id', 'name', 'department', 'desc')

    def to_representation(self, instance):
        """
        修改序列化前的数据格式
        :param instance:
        :return:
        """
        data = super().to_representation(instance)
        if data['department']:
            did = Role.objects.filter(id=data['id']).first()
            if did:
                data['department'] = str(did.department)
                if did.department.pid:
                    data['department'] = str(did.department.pid) + ' / ' + str(did.department)
                    if did.department.pid.pid:
                        data['department'] = str(did.department.pid.pid) + ' / ' + str(did.department.pid) + ' / ' + str(did.department)
                        if did.department.pid.pid.pid:
                            data['department'] = str(did.department.pid.pid.pid) + ' / ' + str(did.department.pid.pid) + ' / ' + str(did.department.pid) + ' / ' + str(did.department)
                            if did.department.pid.pid.pid.pid:
                                data['department'] = str(did.department.pid.pid.pid.pid) + ' / ' + str(did.department.pid.pid.pid) + ' / ' + str(did.department.pid.pid) + ' / ' + str(did.department.pid) + ' / ' + str(did.department)
                                if did.department.pid.pid.pid.pid.pid:
                                    data['department'] = str(did.department.pid.pid.pid.pid.pid) + ' / ' + str(did.department.pid.pid.pid.pid) + ' / ' + str(did.department.pid.pid.pid) + ' / ' + str(did.department.pid.pid) + ' / ' + str(did.department.pid) + ' / ' + str(did.department)
                                    if did.department.pid.pid.pid.pid.pid.pid:
                                        data['department'] = str(did.department.pid.pid.pid.pid.pid.pid) + ' / ' + str(did.department.pid.pid.pid.pid.pid) + ' / ' + str(did.department.pid.pid.pid.pid) + ' / ' + str(did.department.pid.pid.pid) + ' / ' + str(did.department.pid.pid) + ' / ' + str(did.department.pid) + ' / ' + str(did.department)
        else:
            data['department'] = ''

        if data['desc'] is None:
            data['desc'] = ''

        return data
